package com.ait.edu.controller;


import com.ait.commonutils.Msg;
import com.ait.commonutils.exceptionHandler.GuilException;
import com.ait.edu.client.Ossclient;
import com.ait.edu.entity.Video;
import com.ait.edu.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-14
 */
@RestController
@RequestMapping("/eduservice/video")
public class VideoController {


    @Autowired
    private VideoService videoService;

    @Autowired
    private Ossclient ossclient;

    @PostMapping("addVideo")
    public Msg addVideo(@RequestBody Video video){
        if (videoService.save(video)){
            return Msg.success();
        }
        return Msg.fail();
    }

    @DeleteMapping("delVideo")
    public Msg delVideo(String id){
        Video byId = videoService.getById(id);
        String sourceId = byId.getVideoSourceId();
        if (!StringUtils.isEmpty(sourceId)){
            Msg msg = ossclient.delVideo(sourceId);
            if (msg.getCode()==201) {
                 throw new GuilException(404,"删除视频失败");
            }
        }
        if (videoService.removeById(id)) {
            return Msg.success();
        }
        return Msg.fail();
    }

    @GetMapping("getVideo")
    public Msg getVideo(String id){
        Video byId = videoService.getById(id);
        if(byId!=null){
            return Msg.success().data("video",byId);
        }
        return Msg.fail();
    }

    @PutMapping("modifyVideo")
    public Msg modifyVideo(@RequestBody Video video){
        if (videoService.updateById(video)){
            return Msg.success();
        }
        return Msg.fail();
    }

}

